package com.lopo.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;

import com.lopo.util.CreateCodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller
public class CodeController {
    /**
     * @author: Mr.ZJW
     * @date: 2022/4/21 11:51
     * @description: 生成验证码
     */
    @GetMapping("/getCode")
    public void getCode(HttpServletResponse response) throws IOException {
        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36, 5, 4);
        BufferedImage image = (BufferedImage) lineCaptcha.createImage(CreateCodeUtil.getRandomComCode(5));

        ImageIO.write(image, "JPEG", response.getOutputStream());

        //将验证码存入redis


//        //存放到Redis
//        try (ServletOutputStream outputStream = response.getOutputStream()) {
//            //验证码写到页面上
//            lineCaptcha.write(outputStream);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }
}
